/*
	Interface对象的write方法
*/


var readline = require('readline');

var fs = require('fs');

var file = fs.createReadStream('./message.txt');
var out = fs.createWriteStream('./anotherMessage.txt');

var index = 1;

out.write("line" + index.toString() + ":");
index += 1;

var r1 = readline.createInterface({
	input: file,
	output: out,
	terminal: true
});

r1.on('line', function(line) {
	out.write("line" + index.toString() + ":");
	index += 1;
});

file.on('end', function() {
	var buf = new Buffer("文件创建时间：" + (new Date()).toLocaleString());
	// 由于r1的write方法写入的数据将同样被r1读取并触发line事件
	// 为了避免最后一行的行首写入行号，使用r1的removeAllListeners移除r1的line事件回调
	r1.removeAllListeners('line');
	r1.write("\r\n");
	r1.write(buf);
});